// Array of html 5 block-level elements
var html5Elements = [
  'abbr',
  'article',
  'aside',
  'audio',
  'canvas',
  'details',
  'figcaption',
  'figure',
  'footer',
  'header',
  'hgroup',
  'mark',
  'menu',
  'meter',
  'nav',
  'output',
  'progress',
  'section',
  'summary',
  'time',
  'video'
];

var MSIE = /*@cc_on!@*/false;
var IE6 = MSIE && /MSIE 6/i.test(navigator.userAgent);
var KHTML = navigator.userAgent.indexOf('WebKit') == -1 && navigator.userAgent.indexOf('KHTML') != -1 ? true : false;

// Fix html 5 elements for Internet Explorer
function fiks_html_ie(elementList) {
  for (var e = 0; e < elementList.length; e++) {
    document.createElement(elementList[e]);
  }
}
if (MSIE) fiks_html_ie(html5Elements);

// Makes the details element work as expected
/* Note: Requires additional markup to work. The <details> element
   must be wrapped in a <div> to work in Firefox. The contents of
   the details, minus the <summary> element must be wrapped in a <div>. */
function fiks_details_elements() {
  var details_elements = document.getElementsByTagName('details');
  for (var d in details_elements) {
    var details = details_elements[d];
    if (details.nodeType != 1) continue;

    var summary = details.getElementsByTagName('*')[0];
    if (summary.nodeName.toLowerCase() != 'summary') {
      var new_summary = document.createElement('summary');
      new_summary.appendChild(document.createTextNode('Details'));
      details.insertBefore(new_summary, details.firstChild);
      summary = new_summary;
    }

    xbAddEventListener(summary, 'click', toggle_details);
    xbAddEventListener(summary, 'keypress', function(e) {
      key = e.which || e.keyCode;
      if (key == 32 || (key == 13 && !window.opera))
        toggle_details.call(this, e);
    });

    summary.tabIndex = 0;

    // makes IE refresh. Taken from Remy Sharp.
    if (MSIE) setTimeout(function () {
      document.body.className = document.body.className;
    }, 13);

    if (IE6) {
      if (details.getAttribute('open') != null) {
        details.getElementsByTagName('div')[0].style.overflow = 'auto';
        details.getElementsByTagName('div')[0].style.height = 'auto';
      } else {
        details.getElementsByTagName('div')[0].style.overflow = 'hidden';
        details.getElementsByTagName('div')[0].style.height = '0';
      }
    }
  }
}

// Toggle the open/closed state of the <details> element
function toggle_details(e) {
  details = this.parentNode;

  if (details.getAttribute('open') != null) {
    details.removeAttribute('open');
    if (IE6 || KHTML) {
      details.getElementsByTagName('div')[0].style.overflow = 'hidden';
      details.getElementsByTagName('div')[0].style.height = '0';
    }
  } else {
    details.setAttribute('open', 'open');
    if (IE6 || KHTML) {
      details.getElementsByTagName('div')[0].style.overflow = 'auto';
      details.getElementsByTagName('div')[0].style.height = 'auto';
    }
  }

  // makes IE refresh. Taken from Remy Sharp.
  if (MSIE) setTimeout(function () {
    document.body.className = document.body.className;
  }, 13);

  if (!e) var e = window.event;
  e.cancelBubble = true;
  if (e.stopPropagation) e.stopPropagation();
}

function init_html5() {
  fiks_details_elements();
}
xbAddEventListener(window, 'DOMContentLoaded', init_html5);

// cross-browser event registration
function xbAddEventListener(object, event, handler) {
  if (object.addEventListener)
    object.addEventListener(event, handler, false);
  else if (object.attachEvent) {
    event = event == 'DOMContentLoaded' ? 'load' : event;
    object['e'+event+handler] = handler;
    object.attachEvent("on"+event, function(e){object['e'+event+handler](e);});
  }
}

// gotta love how we have to fix everything in IE
if(!Array.indexOf) {
  Array.prototype.indexOf = function(obj) {
    for(var i = 0; i < this.length; i++)
      if(this[i]==obj)
        return i;
    return -1;
  };
}
